package uranium.gui.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import uranium.db.DBManager;

import java.awt.Rectangle;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ViewInfoClienteGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="251,30"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JButton jButton = null;
	private DBManager dbm = null;
	private JLabel jLabelCF = null;
	private String codice = null;
	private JLabel jLabelCognome = null;
	private JLabel jLabelNome = null;
	private JLabel jLabelIndirizzo = null;
	private JLabel jLabelCAP = null;
	private JLabel jLabelCitta = null;
	private JLabel jLabelTel = null;
	private JLabel jLabelFax = null;
	private JLabel jLabelEmail = null;
	private JLabel jLabelNote = null;
	
	public ViewInfoClienteGUI(DBManager dbm, JFrame jFrame, String codice) {
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		this.codice = codice;
		initialize();
	}
	
	private void initialize(){
		//jFrame = new JFrame();
		this.setSize(new Dimension(352, 353));
		this.setTitle("Dati del Cliente");
		this.setPreferredSize(new Dimension(250, 188));
		this.setContentPane(getJContentPane());
		centro();
		settaCampi();
		this.setVisible(true);
		
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabelNote = new JLabel();
			jLabelNote.setBounds(new Rectangle(123, 221, 200, 22));
			jLabelNote.setText("JLabel");
			jLabelEmail = new JLabel();
			jLabelEmail.setBounds(new Rectangle(123, 199, 200, 22));
			jLabelEmail.setText("JLabel");
			jLabelFax = new JLabel();
			jLabelFax.setBounds(new Rectangle(123, 177, 200, 22));
			jLabelFax.setText("JLabel");
			jLabelTel = new JLabel();
			jLabelTel.setBounds(new Rectangle(123, 155, 200, 22));
			jLabelTel.setText("JLabel");
			jLabelCitta = new JLabel();
			jLabelCitta.setBounds(new Rectangle(123, 133, 200, 22));
			jLabelCitta.setText("JLabel");
			jLabelCAP = new JLabel();
			jLabelCAP.setBounds(new Rectangle(123, 111, 200, 22));
			jLabelCAP.setText("JLabel");
			jLabelIndirizzo = new JLabel();
			jLabelIndirizzo.setBounds(new Rectangle(123, 89, 200, 22));
			jLabelIndirizzo.setText("JLabel");
			jLabelNome = new JLabel();
			jLabelNome.setBounds(new Rectangle(123, 67, 200, 22));
			jLabelNome.setText("JLabel");
			jLabelCognome = new JLabel();
			jLabelCognome.setBounds(new Rectangle(123, 45, 200, 22));
			jLabelCognome.setText("JLabel");
			jLabelCF = new JLabel();
			jLabelCF.setBounds(new Rectangle(123, 23, 200, 22));
			jLabelCF.setText("JLabel");
			jLabel9 = new JLabel();
			jLabel9.setText("Note:");
			jLabel9.setBounds(new Rectangle(15, 224, 36, 16));
			jLabel8 = new JLabel();
			jLabel8.setText("e-mail:");
			jLabel8.setBounds(new Rectangle(15, 202, 45, 16));
			jLabel7 = new JLabel();
			jLabel7.setText("Fax:");
			jLabel7.setBounds(new Rectangle(15, 180, 30, 16));
			jLabel6 = new JLabel();
			jLabel6.setText("Telefono:");
			jLabel6.setBounds(new Rectangle(15, 158, 59, 16));
			jLabel5 = new JLabel();
			jLabel5.setText("Citta':");
			jLabel5.setBounds(new Rectangle(15, 136, 39, 16));
			jLabel4 = new JLabel();
			jLabel4.setText("CAP:");
			jLabel4.setBounds(new Rectangle(15, 114, 34, 16));
			jLabel3 = new JLabel();
			jLabel3.setText("Indirizzo:");
			jLabel3.setBounds(new Rectangle(15, 92, 57, 16));
			jLabel2 = new JLabel();
			jLabel2.setText("Nome:");
			jLabel2.setBounds(new Rectangle(15, 70, 43, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("Cognome:");
			jLabel1.setBounds(new Rectangle(15, 48, 64, 16));
			jLabel = new JLabel();
			jLabel.setText(" Codice Fiscale:");
			jLabel.setBounds(new Rectangle(15, 26, 95, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(250, 166));
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(jLabel2, null);
			jPanel.add(jLabel3, null);
			jPanel.add(jLabel4, null);
			jPanel.add(jLabel5, null);
			jPanel.add(jLabel6, null);
			jPanel.add(jLabel7, null);
			jPanel.add(jLabel8, null);
			jPanel.add(jLabel9, null);
			jPanel.add(getJButton(), null);
			jPanel.add(jLabelCF, null);
			jPanel.add(jLabelCognome, null);
			jPanel.add(jLabelNome, null);
			jPanel.add(jLabelIndirizzo, null);
			jPanel.add(jLabelCAP, null);
			jPanel.add(jLabelCitta, null);
			jPanel.add(jLabelTel, null);
			jPanel.add(jLabelFax, null);
			jPanel.add(jLabelEmail, null);
			jPanel.add(jLabelNote, null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("OK");
			jButton.setBounds(new Rectangle(135, 270, 83, 26));
			jButton.setPreferredSize(new Dimension(83, 26));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e22){
						System.err.println(e22);
					}
				}
			});
		}
		return jButton;
	}
	
	private void close(){
		this.dispose();
	}
	
	private int getNewID(){
		ResultSet rs = null;
		int newID = 0;
		String query = "";
		query = "SELECT MAX(id) FROM clienti";
		try{
			rs = dbm.executeQuery(query);
			rs.next();
			newID = (rs.getInt(1)) + 1;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return newID;
	}
	
	private void settaCampi(){
		String query = "SELECT * FROM clienti WHERE id='"+codice+"'";
		try{
			ResultSet rs = dbm.executeQuery(query);
			if(rs.next()){
				jLabelCF.setText(rs.getString("cf"));
				jLabelCognome.setText(rs.getString("cognome"));
				jLabelNome.setText(rs.getString("nome"));
				jLabelIndirizzo.setText(rs.getString("indirizzo"));
				jLabelCAP.setText(rs.getString("cap"));
				jLabelCitta.setText(rs.getString("citta"));
				jLabelTel.setText(rs.getString("tel"));
				jLabelFax.setText(rs.getString("fax"));
				jLabelEmail.setText(rs.getString("email"));
				jLabelNote.setText(rs.getString("note"));
			}			
		}
		catch (SQLException e){
			e.printStackTrace();
		}	
	}
}
